package com.yxx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.yxx.mapper.ChineseWesternMedicineMapper;
import com.yxx.mapper.SetMedicineMapper;
import com.yxx.mapper.ShoppingCartMapper;
import com.yxx.medicinecommon.context.BaseContext;
import com.yxx.medicinepojo.dto.ShoppingCartDTO;
import com.yxx.medicinepojo.entity.ChineseWesternMedicine;
import com.yxx.medicinepojo.entity.SetMedicine;
import com.yxx.medicinepojo.entity.ShoppingCart;
import com.yxx.service.ShoppingCartService;
import jdk.nashorn.internal.runtime.SharedPropertyMap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yuxiangxun
 * @date 2024/9/23
 * @apiNote
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private ChineseWesternMedicineMapper chineseWesternMedicineMapper;

    @Autowired
    private SetMedicineMapper setMedicineMapper;

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCartList() {
        //获取当前微信登录用户id
        Long wx_user_id = BaseContext.getCurrentId();
        //创建shoppingCart对象，将当前微信登录用户id封装到shoppingCart对象
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .id(wx_user_id)
                .build();
        //根据shoppingCart对象动态查询购物车信息
        List<ShoppingCart> list = shoppingCartMapper.ShowShoppingCartList(shoppingCart);
        return list;
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppinigCart(ShoppingCartDTO shoppingCartDTO) {
        //创建shoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();

        log.info("shoppingCartDTO:{}",shoppingCartDTO.toString());

        //对象拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //获取当前登录微信用户id
        Long wx_user_id = BaseContext.getCurrentId();
        log.info("wx_user_id:{}",wx_user_id);

        //将当前微信稿登录用户id封装到shoppingCart对象中
        shoppingCart.setUserId(wx_user_id);

        //通过当前登录用户 ，查询购物车信息（集合）
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.ShowShoppingCartList(shoppingCart);
        log.info("shoppingCartList:{}",shoppingCartList.toString());

        //先判断当前加入购物车中的药品是否已经存在
        //如果有数据（购物车已经存在该药品，只需要数量+1）
        if(shoppingCartList != null && shoppingCartList.size() > 0){
            //因为只能一次点击添加操作，当前集合只有一条数据，获取唯一一条数据作为cart对象
            ShoppingCart cart = shoppingCartList.get(0);
            //通过cart对象，修改number -》 number+1
            cart.setNumber(cart.getNumber()+1);
            //修改这条购物车记录
            shoppingCartMapper.updateNumberById(cart);
        }else{
            //如果没有数据，添加到购物车（判断添加到购物车的是中/西药还是组合药）

            //通过shoppingCartDTO对象中的chinesewesternmedicineId属性获取到当前chinesewesternmedicineId
            Long chinesewesternmedicineId = shoppingCartDTO.getChinesewesternmedicineId();
            if(chinesewesternmedicineId != null){
                //如果chinesewesternmedicineId不为空 -》 中/西药
                ChineseWesternMedicine chineseWesternMedicine = chineseWesternMedicineMapper.ChineseWesternMedicineById(chinesewesternmedicineId);
                //封装name/image/amount到shoppingCart对象
                String chineseWesternMedicineName = chineseWesternMedicine.getName();
                String chineseWesternMedicineImage = chineseWesternMedicine.getImage();
                BigDecimal chineseWesternMedicinePrice = chineseWesternMedicine.getPrice();
                shoppingCart.setName(chineseWesternMedicineName);
                shoppingCart.setImage(chineseWesternMedicineImage);
                shoppingCart.setAmount(chineseWesternMedicinePrice);
            }else{
                //如果chinesewesternmedicineId为空 -》 组合药数据
                Long setmedicineId = shoppingCartDTO.getSetmedicineId();
                //通过组合药id查询组合药信息
                SetMedicine setmedicine = setMedicineMapper.getBySetmedicineId(setmedicineId);
                String setmedicineName = setmedicine.getName();
                String setmedicineImage = setmedicine.getImage();
                BigDecimal setmedicinePrice = setmedicine.getPrice();
                //封装name/image/amount到shoppingCart对象
                shoppingCart.setName(setmedicineName);
                shoppingCart.setImage(setmedicineImage);
                shoppingCart.setAmount(setmedicinePrice);
            }

            //number为1、创建时间 封装到 shoppingCart中
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            //通过封装后的shoppingCart对象插入数据
            shoppingCartMapper.insert(shoppingCart);

        }

    }

    /**
     * 减少一个购物车商品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //创建shoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();

        //对象拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //获取当前微信用户登录id
        Long wx_user_id = BaseContext.getCurrentId();

        //将当前微信登录用户id封装到shoppingCart中
        shoppingCart.setUserId(wx_user_id);

        //根据shoppingCart对象查询购物车信息
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.ShowShoppingCartList(shoppingCart);

        //判断查询出来的记录是否为空
        if(CollectionUtil.isNotEmpty(shoppingCartList) && shoppingCartList.size() > 0){
            //不为空，获取到这条记录 -》 cart
            ShoppingCart cart = shoppingCartList.get(0);

            //获取这条记录的number
            Integer cartNumber = cart.getNumber();

            //如果number == 1 -> 通过这条记录的id直接整个删除这一条记录的信息
            if(cartNumber == 1){
                Long shoppingCartById = cart.getId();
                shoppingCartMapper.deleteShoppingCartById(shoppingCartById);
            }else{
                //number > 1 -> number - 1 后赋值给number 封装回 cart -> 根据这个cart对象更新数据库中这一个记录的信息
                Integer newNumber = cart.getNumber()-1;
                cart.setNumber(newNumber);
                shoppingCartMapper.updateNumberById(cart);
            }


        }


    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteShoppingCart() {
        //获取当前微信登录用户id
        Long wx_user_id = BaseContext.getCurrentId();
        shoppingCartMapper.deleteShoppingCartByUserId(wx_user_id);
    }


}
